<?php

namespace App\Http\Requests;

use App\Models\MedicalRecord;
use App\Models\Patient;
use Illuminate\Database\Query\Builder;
use Illuminate\Validation\Rule;

class PatientUpdateRequest extends PatientBaseInfoRequest
{
    public function phone()
    {
        return MedicalRecord::where(['tn_user_base.phone' => $this->input('phone')])
                ->join('tn_user_base', 'tn_user_base.id', '=', 'st_medical_records.patient_id')
                ->where('tn_user_base.id', '<>', $this->input('patient_id'))
                ->exists();
    }

    protected function medicalNumber()
    {
        if ($this->has('medical_number')) {
            return !Patient::whereMedicalNumber($this->input('medical_number'))
                            ->where('id', '<>', $this->input('patient_id'))->exists();
        }

        return true;
    }

//    protected function cardNumber()
//    {
//        if ($this->has('cardnumber')) {
//            return !Patient::where('CARDNUMBER', $this->input('cardnumber'))
//                            ->where('id', '<>', $this->input('patient_id'))->exists();
//        }
//
//        return true;
//    }

    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules()
    {
        $rule = [
            'name'            => 'alpha_num|max:16',
            'patient_id'      => [
                'required',
                'exists:tn_user_base,id',
            ],
            'record_id'       => [
                'required',
                Rule::exists('st_medical_records', 'id')->where(function (Builder $query) {
                    $query->where('patient_id', $this->input('patient_id'));
                }),
            ],
            'phone'           => [
                'regex:/^1(3|4|5|7|8|9)\\d{9}$/',
                Rule::unique('tn_user_base')
                    ->where('type', 1)
                    ->ignore($this->input('patient_id'))
            ],
            'hospital_id'     => 'integer|min:1|exists:tn_scene,id',
            'doctor_id'       => [
                'integer', 'min:1',
                Rule::exists('tn_user_base', 'id')->where(function (Builder $query) {
                    $query->where('hospital_id', $this->input('hospital_id'));
                }),
            ],
            'sex'             => 'required_with:is_gestation|in:0,1',
            'birthday'        => 'date|before_or_equal:now',
            'clinic_at'       => 'date|before_or_equal:now',
            'medical_number'  => 'string|max:20',
            'diabetes_type'   => 'in:1,2,3',
            'is_gestation'    => 'in:0,1,2',
            'special_illness' => 'in:0,1,2,3,4,5',
            'nation'          => 'integer|min:1|max:56',
            'province'        => ['integer', 'min:0'],
            'city'            => ['integer', 'min:0'],
            'county'          => ['integer', 'min:0'],
        ];

        return $rule;
    }

    public function messages()
    {
        return [
            'name.alpha'                => '患者名称 只能包含汉字、字母、数字。',
            'name.max'                  => '患者名称 最大 :max个字符。',
            'phone.regex'               => '手机号码 格式错误。',
            'phone.unique'              => '手机号码 已被占用。',
            'sex.required_with'         => '提交 是否妊娠时 需要提交患者性别。',
            'sex.in'                    => '提交的患者性别 错误。',
            'doctor_id.exists'          => '该医院没有您选择的医生',
            'clinic_at.required'        => '就诊日期不能为空。',
            'clinic_at.date_format'     => '就诊日期格式不正确，例：1987-04-13',
            'clinic_at.before_or_equal' => '就诊日期 必须要早于现在。',
            'birthday.date_format'      => '出生日期格式不正确，例：1987-04-13',
            'birthday.before_or_equal'  => '出生日期 必须要早于现在。',
            'nation.integer'            => '民族 必须是整数。',
            'nation.min'                => '选择的民族 不存在。',
            'nation.max'                => '选择的民族 不存在。',
            'province.integer'          => '省份 必须是整数。',
            'province.min'              => '省份ID 需要大于等于 :min。',
            'province.exists'           => '省份 不存在。',
            'city.integer'              => '城市 必须是整数。',
            'city.min'                  => '城市 需要大于等于 :min。',
            'city.exists'               => '城市 不存在或不属于选择的省份。',
            'county.integer'            => '区县 必须是整数。',
            'county.min'                => '区县 需要大于等于 :min。',
            'county.exists'             => '区县 不存在或不属于选择的城市。',
        ];
    }
}
